package com.moss.cloud.common.core.utils;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.moss.cloud.common.core.model.vo.AuthUser;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 瑾年
 * @data 2020/03/17 23:51
 */
@Slf4j
@UtilityClass
public final class UserContextHolder {
    private static final ThreadLocal<AuthUser> userInfo = new TransmittableThreadLocal<>();

    public static void clear() {
        userInfo.remove();
    }

    public static void setUserInfo(AuthUser authUser) {
        UserContextHolder.userInfo.set(authUser);
    }

    public static AuthUser getUserInfo() {
        return UserContextHolder.userInfo.get();
    }

    public static Long getTenantId() {
        // 用户不属于任何租户返回租户ID为0
        return userInfo.get().getTenantId();
    }

    public static Long getUserId() {
        return userInfo.get().getUserId();
    }

    public static String getUsername() {
        return userInfo.get().getUserName();
    }

    public static Long getDeptId() {
        return userInfo.get().getDeptId();
    }

    public static List<Long> getRoleIds() {
        return userInfo.get().getRoleIds();
    }
    public static Boolean getAuthStatus() {
        return userInfo.get().getAuthStatus();
    }

    public static List<String> getRoleCodes() {
        return userInfo.get().getRoleCodes();
    }
}